var util = require('../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasMore: true,
    isOpen:false,
    searchValue: '',
    isDefTap: false,
    hasUserInfo: false,
    positionPage: {
      loading: false,
      pageNum: 0,
      now_page_no: 1,
      data: [],
      noMore: false,
      param: ''
    },
    currentTab: 0,
    loadingText: '加载中...',//
    loadingHidden: true,//默认隐藏更多
    banner: {},
    autoplay: true,
    interval: 5000,
    duration: 1000,
    option1: [{
      text: '全部领域',
      value: 0
    }],
    option2: [{
      text: '全部地区',
      value: 0
    }],
    option3: [{
        text: '薪资范围',
        value: 0
      },
      {
        text: '10-20W',
        value: 1
      },
      {
        text: '20-30W',
        value: 2
      },
      {
        text: '30-50W',
        value: 3
      },
      {
        text: '50-100W',
        value: 4
      },
      {
        text: '100W以上',
        value: 5
      },
    ],
    value1: 0,
    value2: 0,
    value3: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (app.globalData.userInfo != null) {
      that.setData({ hasUserInfo: true });
    }
    var param = options.param;
    util.post(app.api.getAdcarouse, { type: 1 }, function (res) {
      //获取position轮播图
      that.setData({
        banner: res.data
      });
    
    });
    that.getCity();
    that.getDomains();
    this.resetData(true,  false, 1);
  },

  getDomains() {
    var that = this;
    var domainList = that.data.option1;
    util.post(app.api.getDomains, {}, function (res) {
      let result = res.data;
      const domains = domainList.concat(result);
      that.setData({
        option1: domains
      })
    })
  },

  getCity() {
    var that = this;

    var cityList = that.data.option2;
    console.log(cityList);
    util.post(app.api.getCities, {}, function (res) {
      let result = res.data;
      const cities = cityList.concat(result);
      that.setData({
        option2: cities
      })
    })
  },

  toggleCityValue: function({ detail }){
    this.setData({
      value2: detail
    })
    this.getJobData();
  },

  toggleSalaryValue: function({ detail }){
    this.setData({
      value3: detail
    })
    this.getJobData();
  },

  toggleDomainValue: function ({ detail }) {
    this.setData({
      value1: detail
    })
    this.getJobData();
  },
  

  getJobData(){
    this.resetData(true);
  },

  searchSubmit: function (e) {
  
    var that = this;
    console.log(e.detail);
    var par = e.detail.value;
    that.setData({
      matchPage: {
        loading: false,
        pageNum: 0,
        now_page_no: 1,
        data: [],
        noMore: false,
        param: par
      },
      searchValue: par
    });
    this.resetData(true);
  },
  //职位详情
  positionDetailTap: function (event) {
    var id = event.currentTarget.dataset.id; // 当前id

    // 切换页面
    wx.navigateTo({
      url: '../position-detail/position-detail?pid=' + id
    });
  },
 
  
  /** 
 * 点击tab切换 
 */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      });
      var isDefTap = true;
      if (e.target.dataset.current != 0) {
        isDefTap = false;
      }
      that.setData({
        isDefTap: isDefTap
      });
    }
  },
  resetDataForPosition: function (url, now_page_no, isappend) {
    var that = this;
    var currObj = that.data.positionPage;
    util.post(url, {
      keyWord: this.data.searchValue,
      domain: this.data.value1,
      city: this.data.value2,
      salary: this.data.value3,
      page: now_page_no, 
      size: 10 }, function (res) {
      if (res.data == null) {
        wx.showToast({
          title: '正在加载中......',
          icon: 'loading',
          duration: 1200
        })
        that.setData({
          isDefTap: true
        })
        return;
      }
      if (isappend) {
        //上拉加载更多
        that.setData({ 'positionPage.loading': false });
        
        var data = [];
        for (var item in currObj.data) {
          data.push(currObj.data[item]);
        }
        for (var item in res.data.data) {
          data.push(res.data.data[item]);
        }

      } else {
        that.setData({
           'positionPage.noMore': false,
            loadingHidden: true
        });
        //下拉刷新 此处停止刷新效果  重置now_page_no为1
        
        wx.stopPullDownRefresh();
        var data = res.data.data;
        //wx.showToast({ title: '刷新了' });
      }
      data.forEach(function(item, index){
        item.createDate = util.date_format_time(item.createDate);
      })
      that.setData({ 'positionPage.pageNum': res.data.pageNum });
      that.setData({ 'positionPage.data': data, isDefTap: true});
      
    });
    that.setData({
      'positionPage.now_page_no' : now_page_no +1
    })
    var currObj = this.data.positionPage;
    if (currObj.now_page_no >= currObj.pageNum){
      that.setData({
        hasMore: false,
      })
    }else{
      that.setData({
        hasMore: true
      })
    }
  },


  resetData: function (isOnLoad, isappend, now_page_no) {
    var isappend = isappend == undefined ? false : true;
    var now_page_no = now_page_no == undefined ? 1 : now_page_no;
    var url = "";
    if (isOnLoad) {

      this.resetDataForPosition(app.api.getJob, now_page_no, isappend);
    } else {
        //最新职位
        url = app.api.getJob;
        this.resetDataForPosition(url, now_page_no, isappend);
    }
    

  },

  onOpen(){
    this.setData({
      isOpen:true
    })
  },
  onClose(){
    this.setData({
      isOpen:false
    })
  },
  ondisble(){
    this.selectComponent('#mydropdown').close();
    this.setData({
      isOpen:false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
    // this.resetData(false);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var currObj = this.data.positionPage;
    if (currObj.loading) return;

    if (currObj.now_page_no >= currObj.pageNum) {
        //最新职位
        this.setData({ 'positionPage.noMore': true });
        this.setData({
          hasMore: false
        })
    } else {
      this.resetData(false,  true, currObj.now_page_no);
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})